#!/usr/bin/env python3
# -*- coding:UTF-8 -*-
#-----------------------------_#
#本部分代码主要实践函数的内容
#-----------------------------_#

# 函数的定义
def yunweinote_001(): #无传参
    print("hello,I am python function.") #函数体内部语句
    return

# 函数的调用
yunweinote_001()


def yunweinote_002(list): #传输参数，参数是Python的全部数据类型或者对象
        print(list)
        return
yunweinote_002([1,2,3,4])
yunweinote_002("yunweinote")

# 函数的返回
def yunweinote_003(str):
    return str #return后面跟量(常量，变量),或者表达式(表达式实际上存在返回值)

list_001 = [1,2,'3','4']
for i in range(len(list_001)): #一个包含了数字和字符的列表
    try:
        print(yunweinote_003(list_001[i]))
    except:
        print("传输参数不对")

# 函数的参数
# 关键字参数
def yunweinote_004(name,age):
    print("name: %s" % name)
    print("age: %d" % age)
    print("name is %s,age is %d" %(name,age))
    return
yunweinote_004(age=28,name="yunweinote")

# 默认参数
def yunweinote_005(name,age=28):
    print("name is %s, and age is %d" %(name,age))
    return
yunweinote_005("yunweinote")
yunweinote_005("yunweinote",82)

# 不定长参数
def yunweinote_006(name,*age):
    print("my name is %s" % name)
    for var in age:
        print("age is %d" % var)
    return
yunweinote_006("yunweinote")
yunweinote_006("yunweinote",10,120,43,68)

# 变量作用域
MyName = "liwanliang"
MyNameList=['l','w','l']
def yunweinote_007():
        myname = "liwanliang_001"
        #print(locals()) #打印函数内部本地作用域

        Myname = "liwanliang_01"
        #print(locals()) 

        MyNameList=['hello','world'] #与全局变量名字一样的局域变量，如果是可变类型，比如list，dict 则可以修改
        print(MyNameList)
        print(tuple(MyNameList))

#print(globals()) #打印程序全局作用域
yunweinote_007()
print(MyName)
try:
        print(myname)
except:
        print("未定义变量")
# 嵌套函数

def yunweinote_008():
        name = "liwanliang"
        age = 28
        def print_name():
                print("My Name is: %s" % name)
        print_name()
        def print_age():
                print("My age is %d" % age)
        print_age()
yunweinote_008()

def yunweinote_009():
        yunweinote_008()
        print("this is function yuweinote_009.")
yunweinote_009()

# 函数是对象
def yunweinote_010():
        pass
print(issubclass(int,object)) #判断是否是对象的函数
print(issubclass(yunweinote_010.__class__,object)) #function.__class__ 表示函数的内嵌属性，返回是函数的属性为对象

def yunweinote_011(a,b):
        return a+b
def yunweinote_012(a,b):
        return a*b
def yunweinote_013(func,a,b): #函数名作为参数传入别的函数
        return func(a,b)
print(yunweinote_013(yunweinote_011,10,20))
print(yunweinote_013(yunweinote_012,10,20))


